package com.danco.training.zaneuskaya.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.danco.training.zaneuskaya.domain.Forum;
import com.danco.training.zaneuskaya.repository.ForumDao;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Repository
public class ForumDaoImpl extends AbstractHibernateDao<Forum, Integer>
		implements ForumDao {

	
	private static final String FORUM_CREATION_DATE = "forumCreationDate";
	private static final String FORUM_TITLE = "forumTitle";

	@Override
	public Forum read(Integer PK) throws HibernateException {
		return super.read(Forum.class, PK);
	}

	@Override
	public List<Forum> readAll() throws HibernateException {
		return super.readAll(Forum.class, FORUM_TITLE);
	}

	@SuppressWarnings("unchecked")
	public List<Forum> findForumsByTitle(String title) throws HibernateException{
		List<Forum> forums = new ArrayList<Forum>();
		Criteria criteria = getSession().createCriteria(Forum.class);
		criteria.add(Restrictions.like(FORUM_TITLE, title));
		
		forums =  modifyCriteria(criteria,FORUM_CREATION_DATE).list();
		return  forums;
	}

		
	@SuppressWarnings("unchecked")
	public List<Forum> sortForumsByCreationDateColumn(String column) throws HibernateException{
		List<Forum> forums = new ArrayList<Forum>();
		Criteria criteria = getSession().createCriteria(Forum.class);
		criteria.addOrder(Order.desc(column));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		forums = criteria.list();
		return forums;
	}

	

}
